home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-17 | 5.9 KB | 251 lines | [TEXT/MMCC] |
- /*
- File: GXDocFile.cp
-
- Contains: The prototype GX doc file
-
- Written by: Jon Summers
-
- Copyright: © 1994 by Jon Summers, all rights reserved.
-
- Change History (most recent first):
-
- */
-
- #include "GXDocFile.h"
- #include "AppLib.h"
-
- #ifndef __QDGXHeaders__
- #include "QDGXHeaders.h"
- #endif
-
- TGXDocFile::TGXDocFile()
- : fPagesInDoc(0)
- {
- }
-
- TGXDocFile::~TGXDocFile()
- {
- }
-
- #if defined (powerpc) || defined (__powerpc)
- #pragma options align=mac68k
- #endif
-
- typedef struct // PrintFileFormat
- {
- long version;
- long offsetPageDir;
- } PrintFileFormat, *PrintFileFormatPtr;
-
- typedef struct // PageDirInfo
- {
- long size;
- long offset;
- } PageDirInfo, *PageDirInfoPtr;
-
- typedef struct // PageDirectory
- {
- long numPages;
- PageDirInfo info[1];
- } PageDirectory, *PageDirPtr;
-
- typedef struct // PrintFileSpool
- {
- gxSpoolBlock spool;
- FSSpec spec;
- OSErr err;
- short fileRef;
- long filePosn;
- } PrintFileSpool, *PrintFileSpoolPtr;
-
- #if defined (powerpc) || defined (__powerpc)
- #pragma options align=reset
- #endif
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- #ifndef KnowsStatic // Object Master
- static
- #endif
- long PrintFileSpoolProc(gxSpoolCommand aSpoolCmd, PrintFileSpoolPtr pBlock)
- {
- OSErr anErr = noErr;
- long aIoCount = pBlock->spool.count;
-
- switch (aSpoolCmd)
- {
- case gxOpenReadSpool:
- anErr = FSpOpenDF(&pBlock->spec, fsRdPerm, &pBlock->fileRef);
- pBlock->err = anErr;
- break;
- case gxOpenWriteSpool:
- anErr = FSpOpenDF(&pBlock->spec, fsWrPerm, &pBlock->fileRef);
- pBlock->err = anErr;
- break;
-
- case gxReadSpool:
- if (pBlock->err)
- anErr = pBlock->err;
- else
- {
- anErr = SetFPos(pBlock->fileRef, fsFromStart, pBlock->filePosn);
- if ( ! anErr)
- {
- anErr = FSRead(pBlock->fileRef, &aIoCount, pBlock->spool.buffer);
- pBlock->err = anErr;
- if (anErr == eofErr)
- anErr = noErr;
- }
- if ( ! anErr)
- pBlock->filePosn += aIoCount;
- else
- pBlock->err = anErr;
- }
- break;
-
- case gxWriteSpool:
- if (pBlock->err)
- anErr = pBlock->err;
- else
- {
- anErr = SetFPos(pBlock->fileRef, fsFromStart, pBlock->filePosn);
- if ( ! anErr)
- anErr = FSWrite(pBlock->fileRef, &aIoCount, pBlock->spool.buffer);
- pBlock->err = anErr;
- if ( ! anErr || (anErr == dskFulErr))
- pBlock->filePosn += aIoCount;
- }
- break;
-
- case gxCloseSpool:
- if (pBlock->fileRef)
- anErr = FSClose(pBlock->fileRef);
- pBlock->fileRef = 0;
- break;
- }
- return anErr;
- }
-
- #ifdef __cplusplus
- }
- #endif
-
- void InitializePrintFileSpool(PrintFileSpoolPtr pBlock);
- void InitializePrintFileSpool(PrintFileSpoolPtr pBlock)
- {
- #ifndef powerpc
- pBlock->spool.spoolProcedure = (gxSpoolProcPtr)PrintFileSpoolProc;
- #else
- pBlock->spool.spoolProcedure = NewgxSpoolProcPtr(PrintFileSpoolProc);
- #endif
- pBlock->err = noErr;
- pBlock->fileRef = 0;
- pBlock->spool.buffer = nil;
- pBlock->spool.bufferSize = 0;
- }
-
- #ifndef powerc
- #define CleanupPrintFileSpool(p)
- #else
- void CleanupPrintFileSpool(PrintFileSpoolPtr pBlock);
- void CleanupPrintFileSpool(PrintFileSpoolPtr pBlock)
- {
- DisposeRoutineDescriptor(pBlock->spool.spoolProcedure);
- }
- #endif
-
- OSErr TGXDocFile::GetDocNumOfPages(void)
- {
- PrintFileSpool aPrFileSpool;
- PrintFileFormat aPrFileFormat;
- PageDirectory aPageDirectory;
-
- // Ref: IM-QD GX: ENv pp7-51,52
- // open data fork
- InitializePrintFileSpool(&aPrFileSpool);
- GetFSSpec(&aPrFileSpool.spec);
- fErr = PrintFileSpoolProc(gxOpenReadSpool, &aPrFileSpool);
- if (fErr) return fErr;
-
- // read PrintFile Format to get offset to page directory
- aPrFileSpool.spool.buffer = &aPrFileFormat;
- aPrFileSpool.spool.count = sizeof(PrintFileFormat);
- aPrFileSpool.filePosn = 0;
- fErr = PrintFileSpoolProc(gxReadSpool, &aPrFileSpool);
- if (fErr) goto Exit_CloseFile;
-
- // read the page directory
- aPrFileSpool.spool.buffer = &aPageDirectory;
- aPrFileSpool.spool.count = sizeof(PageDirectory);
- aPrFileSpool.filePosn = aPrFileFormat.offsetPageDir;
- fErr = PrintFileSpoolProc(gxReadSpool, &aPrFileSpool); // if pagenum is too great we may get eofErr
- if (fErr) goto Exit_CloseFile;
- fPagesInDoc = aPageDirectory.numPages;
-
- /**** Cleanup ****/
- Exit_CloseFile:
- (void)PrintFileSpoolProc(gxCloseSpool, &aPrFileSpool);
- Exit_Cleanup:
- CleanupPrintFileSpool(&aPrFileSpool);
- return fErr;
- }
-
- OSErr TGXDocFile::ReadPageShape(long thePageNum, gxShape* pShape, gxViewPort theViewPort)
- {
- PrintFileSpool aPrFileSpool;
- PrintFileFormat aPrFileFormat;
- PageDirInfo aPgDirInfo;
-
- fErr = GetDocNumOfPages();
- if (fErr)
- return fErr;
-
- // enough pages in directory?
- if (thePageNum > DocNumOfPages())
- {
- fErr = eofErr;
- return fErr;
- }
- // Ref: IM-QD GX: ENv pp7-51,52
- // open data fork
- InitializePrintFileSpool(&aPrFileSpool);
- GetFSSpec(&aPrFileSpool.spec);
- fErr = PrintFileSpoolProc(gxOpenReadSpool, &aPrFileSpool);
- if (fErr) return fErr;
-
- // read PrintFile Format to get offset to page directory
- aPrFileSpool.spool.buffer = &aPrFileFormat;
- aPrFileSpool.spool.count = sizeof(PrintFileFormat);
- aPrFileSpool.filePosn = 0;
- fErr = PrintFileSpoolProc(gxReadSpool, &aPrFileSpool);
- if (fErr) goto Exit_CloseFile;
-
- // read the page directory
- aPrFileSpool.spool.buffer = &aPgDirInfo;
- aPrFileSpool.spool.count = sizeof(PageDirInfo);
- aPrFileSpool.filePosn = aPrFileFormat.offsetPageDir;
- aPrFileSpool.filePosn += sizeof(long);
- aPrFileSpool.filePosn += (thePageNum-1)*sizeof(PageDirInfo);
- fErr = PrintFileSpoolProc(gxReadSpool, &aPrFileSpool); // if pagenum is too great we may get eofErr
- if (fErr) goto Exit_CloseFile;
-
- //close the file: we have offset of flat shape in aPgDirInfo.offset and its flat size in aPgDirInfo.size
- fErr = PrintFileSpoolProc(gxCloseSpool, &aPrFileSpool);
- if (fErr) goto Exit_Cleanup;
-
- InitializePrintFileSpool(&aPrFileSpool);
- aPrFileSpool.filePosn = aPgDirInfo.offset;
- *pShape = GXUnflattenShape(&aPrFileSpool.spool, 1, &theViewPort);
- if (*pShape == nil)
- fErr = GXGetGraphicsError(nil);
- goto Exit_Cleanup;
-
- /**** Cleanup ****/
- Exit_CloseFile:
- (void)PrintFileSpoolProc(gxCloseSpool, &aPrFileSpool);
- Exit_Cleanup:
- CleanupPrintFileSpool(&aPrFileSpool);
- return fErr;
- }